package problem04_10;

//面试题04.10.检查子树
//https://leetcode.cn/problems/check-subtree-lcci/

class Solution {
    public boolean checkSubTree(TreeNode t1, TreeNode t2) {
        if(check(t1, t2)) return true;
        if(t1 == null) return false;
        boolean ret1 = checkSubTree(t1.left, t2);
        if(ret1) return true;
        boolean ret2 = checkSubTree(t1.right, t2);
        if(ret2) return true;
        return false;
    }

    public boolean check(TreeNode root1, TreeNode root2) {
        if(root1 == null && root2 == null) return true;
        if(root1 == null || root2 == null) return false;
        if(root1.val != root2.val) return false;
        if(!check(root1.left, root2.left) || (!check(root1.right, root2.right))) return false;
        return true;
    }
}